package com.hsy.swu.home.exam

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName
import org.json.JSONObject
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

@Entity
class ExamModel {
    @SerializedName("kcmc")
    @ColumnInfo("courseName")
    var courseName: String = ""

    @SerializedName("kssj")
    @Ignore
    var kssj: String = ""

    @ColumnInfo("date")
    var date: String = ""

    @ColumnInfo("time")
    var time: String = ""

    @SerializedName("cdmc")
    @ColumnInfo("examRoom")
    var examRoom: String = ""

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

    override fun toString(): String {
        return "$courseName\t$date\t$time\t$examRoom"
    }

    fun getExamRemainingDay(): Int {
        val examDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        return ChronoUnit.DAYS.between(LocalDate.now(), examDate).toInt()
    }

    companion object {
        // 根据是否考完及时间远近来设置位置
        fun sortByGapDay(toSortList: List<ExamModel>): List<ExamModel> {
            val resultList = mutableListOf<ExamModel>()
            toSortList.forEach {
                if (it.getExamRemainingDay() >= 0) {
                    resultList.add(0, it)
                } else {
                    resultList.add(it)
                }
            }
            return resultList
        }
    }
}